package com.yumeko.service.impl;

import com.yumeko.dao.ResumeDao;
import com.yumeko.pojo.Resume;
import com.yumeko.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;

/**
 * @program: ssp
 * @description:
 * @author: Zheng Hao
 * @create: 2020-01-14 23:38
 */
@Service
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    private ResumeDao resumeDao;

    @Override
    public Page<Resume> doFindResume(String page,String limit) {
        Sort sort = new Sort(Sort.Direction.ASC, "userId");
        Pageable pageable = PageRequest.of(Integer.parseInt(page)-1, Integer.parseInt(limit), sort);
        return resumeDao.findAll(pageable);
    }

    @Override
    public Integer doInsetResume(String userName,String password) {
        return resumeDao.InsetResume(userName,password);
    }

    @Override
    public Integer doUpdateResume(Integer userId,String userName,String password) {
        return resumeDao.UpdateResume(userId,userName,password);
    }

    @Override
    public void doDeleteResume(Integer userId) {
        resumeDao.deleteById(userId);
    }

    @Override
    public Boolean doLogin(String userName, String password, HttpSession session) {
        Resume byUserNameAndPassword = resumeDao.findByUserNameAndPassword(userName, password);
        System.out.println(byUserNameAndPassword);
        if (ObjectUtils.isEmpty(byUserNameAndPassword)) {
            return false;
        }else{
            session.setAttribute("LoginName",byUserNameAndPassword.getUserName());
            return true;
        }
    }

}
